package com.example.cinema.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.databinding.ActivitySpreadBinding;
import com.example.cinema.toolclass.ORCode;
import com.example.cinema.toolclass.ShareTool;
import com.example.cinema.toolclass.ViewBitmapFile;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Activity_spread extends AppCompatActivity {

    private ActivitySpreadBinding ASB;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ASB = DataBindingUtil.setContentView(this, R.layout.activity_spread);
        init();
        initListener();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    private UserAttribute attribute;
    private void init() {
        attribute = (UserAttribute) getApplication();
        ORCode orCode = new ORCode();
        Bitmap bitmap = orCode.CreateORImage(attribute.getDowurl());
        if(bitmap==null)
        {
            Log.i("WWWW","完蛋玩意"+attribute.getDowurl());
            Log.i("WWWW","完蛋玩意"+attribute.getDowbag());
            return;
        }
        ASB.orcode.setImageBitmap(bitmap);
        BitmapBackground(attribute.getDowbag());
    }
    //初始化监听
    private void initListener()
    {
        //返回
        ASB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //保存到相册
        ASB.keep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               File file = new File(getFilesDir(),"spread.png");
                try {
                    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),"spread",null);
                    Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        //分享到QQ
        ASB.spreadqq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareTool.ShareQQ(getApplicationContext(),"推荐给你一款好看的影片软件"+attribute.getDowurl()+",打不开的话复制到浏览器打开哦！");
            }
        });
        //分享到微信
        ASB.spreadwx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareTool.ShareWX(getApplicationContext(),"推荐给你一款好看的影片软件"+attribute.getDowurl()+",打不开的话复制到浏览器打开哦！");
            }
        });
    }

    //获取背景海报数据
    private void  BitmapBackground(String url)
    {
        new Thread()
        {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).get().build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        InputStream stream =  response.body().byteStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(stream);
                        Message message = new Message();
                        message.obj = bitmap;
                        message.what = 1000;
                        handler.sendMessage(message);
                    }
                });
            }
        }.start();
    }
    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1000)
            {
                Bitmap bitmap = (Bitmap) msg.obj;
                ASB.orback.setImageBitmap(bitmap);
                Log.i("AAAA","执行渲染");
                ViewBitmapFile viewBitmapFile = new ViewBitmapFile();
                viewBitmapFile.convert(ASB.orlayout,getApplicationContext());
            }
        }
    };
}
